/*
 * @Description:
 * @Author: zoeblow
 * @Email: zoeblow@163.com
 * @Date: 2020-05-15 22:19:49
 * @LastEditors: zoeblow
 * @LastEditTime: 2020-05-15 23:03:05
 * @FilePath: \libs\static\script\index.js
 */

var timestamp = new Date().getTime();
$.ajax({
  url: "package.json?v=" + timestamp,
  dataType: "json",
  success: function (res) {
    console.log(res);
    $(".g-content").html(template("libs", res));
  },
});

// from https://30secondsofcode.org
function copyToClipboard(str) {
  const el = document.createElement("textarea");
  el.value = str;
  el.setAttribute("readonly", "");
  el.style.position = "absolute";
  el.style.left = "-9999px";
  document.body.appendChild(el);

  const selected =
    document.getSelection().rangeCount > 0
      ? document.getSelection().getRangeAt(0)
      : false;

  el.select();
  document.execCommand("copy");
  document.body.removeChild(el);

  if (selected) {
    document.getSelection().removeAllRanges();
    document.getSelection().addRange(selected);
  }
}

$(".g-content").off("mouseenter", "li");
$(".g-content").on("mouseenter", "li", function (event) {
  var text = $(this),
    range,
    selection;
  if (!text.length) return;
  text = text[0];
  if (document.body.createTextRange) {
    range = document.body.createTextRange();
    range.moveToElementText(text);
    range.select();
  } else if (window.getSelection) {
    selection = window.getSelection();
    range = document.createRange();
    range.selectNodeContents(text);
    selection.removeAllRanges();
    selection.addRange(range);
  }
});

$(".g-content").on("click", "li", function (event) {
  var text = $(this),
    range,
    selection;
  if (!text.length) return;
  var txt = text.html();
  copyToClipboard(txt);
  $(".toast").fadeIn();
  setTimeout(() => {
    $(".toast").fadeOut();
  }, 1500);
});
